var keyNum=0;
var photoNum=0;
var House = function () {

	var handleHouse = function() {
		$('#HouseForm').validate({
			errorElement: 'span', //default input error message container
			errorClass: 'help-block', // default input error message class
			focusInvalid: false, // do not focus the last invalid input
			ignore: "",
			onkeyup: function(element) {
				$(element).valid();
			},
			onfocusout: function(element) {
				$(element).valid();
			},
			rules: {
				cityArea: {
					required: true,
					maxlength:50
				},
				district: {
					required: true,
					maxlength:50
				},
				housesName: {
					required: true
				},
				periods: {
					required: true,
					min:1,
					maxlength:3
				},
				posBuilding: {
					required: true,
					min:1,
				},
				posUnit: {
					required: true,
					min:1,
				},
				posFloor: {
					required: true,
					min:1,
				},
				posRoomNo: {
					required: true,
					min:1,
				},
				posFloorTotal: {
					required: true,
					min:1,
				},
				houseRomNum: {
					required: true,
					min:0,
				},
				houseHallNum: {
					required: true,
					min:0,
				},
				houseToiletNum: {
					required: true,
					min:0,
				},
				houseBalconyNum: {
					required: true,
					min:0,
				},
				houseCarPlace: {
					required: true,
					min:0,
				},
				houseArea: {
					required: true,
					isDecimal: true,
					min:0,
				},
				orientations: {
					required: true
				},
				houseUsage: {
					required: true
				},
				decoration: {
					required: true
				},
				rentManagerName: {
					required: true
				},
				sellManagerName: {
					required: true
				},
				rentManagerShop: {
					required: true
				},
				sellManagerShop: {
					required: true
				},
				rentStatus: {
					required: true
				},
				sellStatus: {
					required: true
				},
				rentPrice: {
					required: true,
					isDecimal: true,
				},
				sellPrice: {
					required: true,
					isDecimal: true,
				},
				registerPrice: {
					required: true,
					isDecimal: true,
				},
				minPirce: {
					required: true,
					isDecimal: true,
				},
				buildingAge:{
					min:0,
				},
				source: {
					required: true
				},
				dutyShop: {
					required: true
				},
				dutyStaff: {
					required: true
				},
				entrustTime: {
					required: true
				},
				customerType: {
					required: true
				},
				keyList: {
					required: true,
					isNumber: true,
				},
				remark: {
					required: true,
					maxlength:600
				},
				propertyCerYear: {
					required: true
				},
				houseLoanBalance: {
					required: true,
					isNumber: true,
				},
				propertyOwnerName: {
					required: true
				},
				nativePlace: {
					required: true
				},
				ownerCerNo: {
					required: true,
					isIdCard :true,
				},
				ownerLinkPhoneNum: {
					required: true,
					isPhone: true,
				},
				propertyRemark: {
					maxlength:300
				},
				agentName: {
					required: true
				},
				agentNativePlace: {
					required: true
				},
				agentCerNo: {
					required: true,
					isIdCard :true,
				},
				agentLinkPhoneNum: {
					required: true,
					isPhone: true,
				}
			},
			messages: {// custom messages for radio buttons and checkboxes
			},
			invalidHandler: function(event, validator) { //display error alert on form submit
				layer.alert("提交前请检查下输入信息！",{title:" "});
			},
			highlight: function(element) { // hightlight error inputs
				$(element).parent().addClass('has-error'); // set error class to the control group
			},
			success: function(label) {
				label.parent().removeClass('has-error');
				label.remove();
			},
			errorPlacement: function(error, element) {
				error.insertAfter(element);
			}
		});
	}
	var handleFolow = function() {
		$('#FolowForm').validate({
			errorElement: 'span', //default input error message container
			errorClass: 'help-block', // default input error message class
			focusInvalid: false, // do not focus the last invalid input
			ignore: "",
			onkeyup: function(element) {
				$(element).valid();
			},
			onfocusout: function(element) {
				$(element).valid();
			},
			rules: {
				folowContent: {
					required: true
				}
			},
			messages: {// custom messages for radio buttons and checkboxes
			},
			invalidHandler: function(event, validator) { //display error alert on form submit
				layer.alert("提交前请检查下输入信息！",{title:" "});
			},
			highlight: function(element) { // hightlight error inputs
				$(element).parent().addClass('has-error'); // set error class to the control group
			},
			success: function(label) {
				label.parent().removeClass('has-error');
				label.remove();
			},
			errorPlacement: function(error, element) {
				error.insertAfter(element);
			}
		});
	}
	var handleFile=function(){
		var up=$("#upload").Huploadify({
			auto:true,
			fileTypeExts:'*.jpg;*.png',
			multi:false,
			fileObjName:'Filedata',
			fileSizeLimit:2097152,
			showUploadedPercent:false,
			buttonText:'<i class="fa fa-plus"></i> 添加房屋照片',
			uploader:"uploadify.json",
			onUploadSuccess:function(file,data){
				var Data=JSON.parse(data);
				if(Data.result==true){
					var t=JSON.parse(Data.t);
					var code= t.code;
					if(code=="0"){
						var $template = $('#photoTemplate');
						$template.find(".imgId").val(t.attach);
						$template.find(".imgName").val(Data.msg);
						$template.find(".viewPhotoButton").attr('id',t.attach);
						var $clone = $template
							.clone()
							.removeClass('hide')
							.removeAttr('id');
						$('#photoDiv').append($clone);
						photoNum++;
					}
				}else{
					layer.alert(Data.msg,{title: " "});
				}
			},
			onUploadError:function(file,response){
				layer.alert("上传失败!",{title: " "});
			}
		});
	}
	var handleKey = function(){

		$('#HouseForm').on('click', '.addKeyButton', function() {
			$("#keyNotice").hide();
			if(keyNum>=1){
				layer.alert("仅限添加一把钥匙!",{title: " "});
			}else{
				var $template = $('#keyTemplate');
				var $clone = $template
					.clone()
					.removeClass('hide')
					.removeAttr('id');
				$('#keyDiv').append($clone);
				//$clone.insertBefore($template);
				keyNum++;
			}
		});
		$('#HouseForm').on('click', '.removeKeyButton', function() {
			var $row = $(this).parents('.form-line');
			keyNum--;
			$row.remove();
		});
	}
	var handlePhoto = function(){

		$('#HouseForm').on('click', '.removePhotoButton', function() {
			var $row = $(this).parents('.form-line');
			photoNum--;
			$row.remove();
		});
		$('#HouseForm').on('click', '.viewPhotoButton', function() {
			var $row = $(this).parents('.form-line');
			var name=$row.find(".imgName").val();
			$("#img-title").html(name);
			var id=$(this)[0].id;
			var url="http://hczlrent.yunjianvc.com/public/previewImg?id="+id;
			document.getElementById("detailImg").src = url;
			$("#ImgModal").modal('show');
		});
		$('#ViewImgs').on('click', '.viewPhotoButton', function() {
			var $row = $(this).parents('.form-line');
			var name=$row.find(".imgName").val();
			$("#img-title").html(name);
			var id=$(this)[0].id;
			var url="http://hczlrent.yunjianvc.com/public/previewImg?id="+id;
			document.getElementById("detailImg").src = url;
			$("#ImgModal").modal('show');
		});
	}

    return {
        //main function to initiate the module
        init: function () {
            handleHouse();
			handleFolow();
			handleKey();
			handlePhoto();
			handleFile();
        }

    };

}();
